Page({
  data: {
    keyword: '',
    questionCount: 0,
    wrongCount: 0,
    examCount: 0,
    collectCount: 0,
    exercises: [
      {
        id: 1,
        name: '第1期: office的基础知识介绍',
        description: '主要包含Office软件的基础操作和常用功能',
        done: 6,
        total: 6
      },
      {
        id: 2,
        name: '第2期: WORD要点三部曲',
        description: '字体必须对照原片文件更改',
        done: 1,
        total: 2
      }
    ],
    selectedCourse: '电子电路EDA技术',
    courses: ['计算机基础知识', '电子电路EDA技术'],
    menuItems: [
      {
        icon: '/images/question-bank.png',
        text: '题库'
      },
      {
        icon: '/images/wrong.png',
        text: '错题'
      },
      {
        icon: '/images/exam.png',
        text: '考试'
      },
      {
        icon: '/images/collect.png',
        text: '收藏'
      }
    ],
    practiceItems: [
      {
        title: '电子电路EDA的功能',
        progress: '0/0',
        status: '答题'
      },
      {
        title: '命令格式及操作使用方法',
        progress: '0/0',
        status: '答题'
      }
    ]
  },

  onLoad() {
    this.loadExercises()
    this.loadCounts()
  },

  onPullDownRefresh() {
    this.loadExercises()
    this.loadCounts()
    wx.stopPullDownRefresh()
  },

  loadExercises() {
    // 加载练习列表
    // wx.request({
    //   url: 'your-api-url/exercises',
    //   success: (res) => {
    //     this.setData({
    //       exercises: res.data
    //     })
    //   }
    // })
  },

  loadCounts() {
    // 加载各项统计数据
    this.setData({
      questionCount: 100,
      wrongCount: 20,
      examCount: 5,
      collectCount: 10
    })
  },

  onSearch(e) {
    const keyword = e.detail.value
    this.setData({ keyword })
    // 实现搜索功能
    // this.searchExercises(keyword)
  },

  searchExercises(keyword) {
    // 根据关键词搜索练习
    // wx.request({
    //   url: `your-api-url/exercises/search?keyword=${keyword}`,
    //   success: (res) => {
    //     this.setData({
    //       exercises: res.data
    //     })
    //   }
    // })
  },

  startPractice(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/exercise/exercise?id=${id}`
    })
  },

  goToQuestionBank() {
    wx.navigateTo({
      url: '/pages/question-bank/question-bank'
    })
  },

  goToWrongQuestions() {
    wx.navigateTo({
      url: '/pages/wrong-questions/wrong-questions'
    })
  },

  goToExam() {
    wx.navigateTo({
      url: '/pages/exam/exam'
    })
  },

  goToCollection() {
    wx.navigateTo({
      url: '/pages/collection/collection'
    })
  },

  // 切换选中的课程
  onCourseChange(e) {
    const course = this.data.courses[e.detail.value]
    this.setData({
      selectedCourse: course
    })
    // 根据选中的课程加载对应的练习项
    this.loadPracticeItems(course)
  },

  // 加载练习项
  loadPracticeItems(course) {
    // 这里可以根据课程从服务器加载对应的练习项
    // 目前使用模拟数据
    if (course === '计算机基础知识') {
      this.setData({
        practiceItems: [
          {
            title: '第1期: office的基础知识介绍',
            progress: '6/6',
            status: '答题'
          },
          {
            title: '第2期: WORD要点三部曲 字体必须对照原片文件更改',
            progress: '1/2',
            status: '答题'
          }
        ]
      })
    }
  },

  // 点击菜单项
  onMenuItemTap(e) {
    const index = e.currentTarget.dataset.index
    const types = ['question-bank', 'wrong-questions', 'exam', 'collection']
    wx.navigateTo({
      url: `/pages/${types[index]}/${types[index]}`
    })
  },

  // 点击练习项
  onPracticeItemTap(e) {
    const index = e.currentTarget.dataset.index
    wx.navigateTo({
      url: `/pages/exercise/exercise?index=${index}&course=${this.data.selectedCourse}`
    })
  }
}) 